package com.dianxin.smartpark.service.property.impl;

import java.util.List;

import com.chinatelecom.response.common.CommonResponseBody;
import com.chinatelecom.response.utils.BeanUtil;
import com.chinatelecom.response.utils.ResponseBodyWrapperUtil;
import com.chinatelecom.response.constant.ErrMsgEnum;

import com.chinatelecom.response.utils.StringUtils;
import com.dianxin.smartpark.dao.dataobject.property.SmartParkPropertyCleaning;
import com.dianxin.smartpark.dao.dataobject.property.SmartParkPropertyCleaningExample;
import com.dianxin.smartpark.dao.mapper.property.SmartParkPropertyCleaningMapper;
import com.dianxin.smartpark.dao.qo.property.SmartParkPropertyCleaningQO;
import com.dianxin.smartpark.manager.order.ISmartParkOrderRecordService;
import com.dianxin.smartpark.service.property.SmartParkPropertyCleaningService;
import com.dianxin.smartpark.service.utils.OrderNoUtil;
import com.dianxin.smartpark.service.vo.property.SmartParkPropertyCleaningVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 保洁管理业务层处理
 * 
 * @author chenjiasong
 * @date 2021-07-22
 */
@Service
public class SmartParkPropertyCleaningServiceImpl implements SmartParkPropertyCleaningService
{
    private final static Logger logger = LoggerFactory.getLogger(SmartParkPropertyCleaningServiceImpl.class);
    @Resource
    private SmartParkPropertyCleaningMapper smartParkPropertyCleaningMapper;

    @Resource
    private ISmartParkOrderRecordService iSmartParkOrderRecordService;

    /**
     * 查询保洁管理
     *
     * @param qo 保洁管理ID
     * @return 保洁管理
     */
    @Override
    public CommonResponseBody<SmartParkPropertyCleaningVO> selectSmartParkPropertyCleaning(SmartParkPropertyCleaningQO qo)
    {
        if(StringUtils.isNull(qo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkPropertyCleaningExample smartParkPropertyCleaningExample = new SmartParkPropertyCleaningExample();
        SmartParkPropertyCleaningExample.Criteria criteria = smartParkPropertyCleaningExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        criteria.andIsDeletedEqualTo(0);
        criteria.andIdEqualTo(qo.getId());
        List<SmartParkPropertyCleaning> passRecords = smartParkPropertyCleaningMapper.selectByExample(smartParkPropertyCleaningExample);
        return ResponseBodyWrapperUtil.successWrapper(StringUtils.isNotEmpty(passRecords)?passRecords.get(0):null,SmartParkPropertyCleaningVO.class);
    }

    /**
     * 查询保洁管理列表
     * 
     * @param qo 保洁管理
     * @return 保洁管理
     */
    @Override
    public CommonResponseBody<List<SmartParkPropertyCleaningVO>> selectSmartParkPropertyCleaningList(SmartParkPropertyCleaningQO qo)
    {
        SmartParkPropertyCleaningExample smartParkPropertyCleaningExample = new SmartParkPropertyCleaningExample();
        SmartParkPropertyCleaningExample.Criteria criteria = smartParkPropertyCleaningExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        criteria.andIsDeletedEqualTo(0);
        if (qo.getNeedPage()) {
            smartParkPropertyCleaningExample.setOrderByClause("gmt_create desc limit " + (qo.getPageIndex() - 1) * qo.getPageSize() + "," + qo.getPageSize());
        } else {
            smartParkPropertyCleaningExample.setOrderByClause("gmt_create desc");
        }
        List<SmartParkPropertyCleaning> passRecords = smartParkPropertyCleaningMapper.selectByExample(smartParkPropertyCleaningExample);
        long count = smartParkPropertyCleaningMapper.countByExample(smartParkPropertyCleaningExample);
        return ResponseBodyWrapperUtil
                .successListWrapper(passRecords, count, qo, SmartParkPropertyCleaningVO.class);
    }

    /**
     * 新增保洁管理
     * 
     * @param vo 保洁管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResponseBody<Boolean> insertSmartParkPropertyCleaning(SmartParkPropertyCleaningVO vo)
    {
        // 验证 todo
        SmartParkPropertyCleaning smartParkPropertyCleaning = new SmartParkPropertyCleaning();
        try {
            BeanUtil.insertConversion(vo, smartParkPropertyCleaning);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkPropertyCleaning.setStatus(0);
        String orderNo = OrderNoUtil.getOrderNo("BJ");
        smartParkPropertyCleaning.setOrderNo(orderNo);
        smartParkPropertyCleaning.beforeInsert();
        smartParkPropertyCleaning.setCreator("1435058739601235968");
        int res = smartParkPropertyCleaningMapper.insertSelective(smartParkPropertyCleaning);
        if(res > 0){
            res = iSmartParkOrderRecordService.insertSmartParkOrderRecord(vo.getTenantId(),0,vo.getCreator(),smartParkPropertyCleaning.getId(),1,1,"提交","提交");
        }
        return ResponseBodyWrapperUtil.toResData(res);
    }

    /**
     * 修改保洁管理
     * 
     * @param vo 保洁管理
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> updateSmartParkPropertyCleaning(SmartParkPropertyCleaningVO vo)
    {
        // 验证 todo
        SmartParkPropertyCleaning smartParkPropertyCleaning = new SmartParkPropertyCleaning();
        try {
            BeanUtil.insertConversion(vo, smartParkPropertyCleaning);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkPropertyCleaning.beforeUpdate();
        int res = smartParkPropertyCleaningMapper.updateByPrimaryKeySelective(smartParkPropertyCleaning);
        return ResponseBodyWrapperUtil.toResData(res);
    }

    /**
     * 删除保洁管理信息
     *
     * @param vo 保洁管理
     * @return 结果08
     */
    @Override
    public CommonResponseBody<Boolean> deleteSmartParkPropertyCleaning(SmartParkPropertyCleaningVO vo)
    {
        // 验证 todo
        if(StringUtils.isNull(vo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkPropertyCleaning smartParkPropertyCleaning = new SmartParkPropertyCleaning();
        try {
            BeanUtil.insertConversion(vo, smartParkPropertyCleaning);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkPropertyCleaning.beforeDelete();
        return ResponseBodyWrapperUtil.toResData(smartParkPropertyCleaningMapper.updateByPrimaryKeySelective(smartParkPropertyCleaning));
    }
}
